/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.hop.workflow.actions.actionplugins;

import org.apache.hop.core.exception.HopException;
import org.apache.hop.core.row.IRowMeta;
import org.apache.hop.core.row.RowDataUtil;
import org.apache.hop.pipeline.Pipeline;
import org.apache.hop.pipeline.PipelineMeta;
import org.apache.hop.pipeline.transform.BaseTransform;
import org.apache.hop.pipeline.transform.ITransform;
import org.apache.hop.pipeline.transform.TransformMeta;

/** Transform that will abort after having seen 'x' number of rows on its input. */
public class ActionPlugins extends BaseTransform<ActionPluginsMeta, ActionPluginsData>
    implements ITransform {
  private static final Class<?> PKG = ActionPlugins.class; // For Translator

  private String inputMessage;
  private String outputMessage;
  private String llmName;

  public ActionPlugins(
      TransformMeta transformMeta,
      ActionPluginsMeta meta,
      ActionPluginsData data,
      int copyNr,
      PipelineMeta pipelineMeta,
      Pipeline pipeline) {
    super(transformMeta, meta, data, copyNr, pipelineMeta, pipeline);
    IRowMeta inputMeta = getInputRowMeta();
    if (inputMeta == null) logBasic("inputMeta = null ");
  }

  @Override
  public boolean init() {
    if (!super.init()) {
      return false;
    }
    llmName = meta.getLlmName();
    inputMessage = meta.getInputMessage();
    outputMessage = meta.getOutputMessage();
    logBasic("[init]xml input===" + meta.getInputMessage());
    logBasic("[init]xml output===" + meta.getInputMessage());
    logBasic("[init]xml llmname===" + meta.getLlmName());
    IRowMeta inputMeta = getInputRowMeta();
    if (inputMeta == null) logBasic("inputMeta = null ");
    return true;
  }

  @Override
  public boolean processRow() throws HopException {
    IRowMeta inputMeta = getInputRowMeta();
    if (inputMeta == null) logBasic("[processRow]#######inputMeta = null ");
    if (first) {
      first = false;
      logBasic("[processRow]#######this.data.outputRowMeta = null ");
      //      this.data.outputRowMeta = inputMeta.clone();
      //      this.data.outputRowMeta.addValueMeta(
      //          new ValueMetaDate("return_char", IValueMeta.TYPE_STRING));
      //      this.data.outputRowMeta.addValueMeta(
      //          new ValueMetaDate("return_code", IValueMeta.TYPE_INTEGER));
    } else {
      logBasic("[processRow]#######this.data.outputRowMeta != null ");
    }
    Object[] rowInput = getRow();
    if (rowInput == null) {
      setOutputDone();
      return false;
    }
    logBasic("row len= " + rowInput.length);
    for (Object ob : rowInput) {
      if (ob != null) logBasic("rowInput===" + ob);
    }
    logBasic("[processRow]input===" + this.meta.getInputMessage());
    logBasic("[processRow]output===" + this.meta.getInputMessage());
    logBasic("[processRow]llmname===" + this.meta.getLlmName());

    logBasic("inputMeta= " + inputMeta.toStringMeta());
    rowInput[0] = "11111";
    Object[] output = {"return chat", 200};
    if (this.data.outputRowData == null) {
      logBasic("this.data.outputRowData= null ");
      this.data.outputRowData = RowDataUtil.addRowData(rowInput, rowInput.length, output);
    } else {
      logBasic("this.data.outputRowData=  " + this.data.outputRowData.length);
      System.arraycopy(rowInput, 0, this.data.outputRowData, 0, rowInput.length);
      System.arraycopy(rowInput, 0, this.data.outputRowData, rowInput.length, output.length);
    }
    if (this.data.outputRowMeta == null) {
      logBasic("[addProperty] output= null");
    }
    putRow(this.data.outputRowMeta, this.data.outputRowData);
    return true;
  }
}
